home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / jpeglib5b / jdcoefct.c < prev    next >
C/C++ Source or Header  |  1980-01-12  |  13KB  |  379 lines

  1. /*
  2.  * jdcoefct.c
  3.  *
  4.  * Copyright (C) 1994-1995, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains the coefficient buffer controller for decompression.
  9.  * This controller is the top level of the JPEG decompressor proper.
  10.  * The coefficient buffer lies between entropy decoding and inverse-DCT steps.
  11.  */
  12.  
  13. #define JPEG_INTERNALS
  14. #include "jinclude.h"
  15. #include "jpeglib.h"
  16.  
  17.  
  18. /* Private buffer controller object */
  19.  
  20. typedef struct {
  21.   struct jpeg_d_coef_controller pub; /* public fields */
  22.  
  23.   JDIMENSION iMCU_row_num;    /* iMCU row # within image */
  24.   JDIMENSION mcu_ctr;        /* counts MCUs processed in current row */
  25.   int MCU_vert_offset;        /* counts MCU rows within iMCU row */
  26.   int MCU_rows_per_iMCU_row;    /* number of such rows needed */
  27.  
  28.   /* In single-pass modes without block smoothing, it's sufficient to buffer
  29.    * just one MCU (although this may prove a bit slow in practice).
  30.    * We allocate a workspace of MAX_BLOCKS_IN_MCU coefficient blocks,
  31.    * and let the entropy decoder write into that workspace each time.
  32.    * (On 80x86, the workspace is FAR even though it's not really very big;
  33.    * this is to keep the module interfaces unchanged when a large coefficient
  34.    * buffer is necessary.)
  35.    * In multi-pass modes, this array points to the current MCU's blocks
  36.    * within the virtual arrays.
  37.    */
  38.   JBLOCKROW MCU_buffer[MAX_BLOCKS_IN_MCU];
  39.  
  40.   /* In multi-pass modes, we need a virtual block array for each component. */
  41.   jvirt_barray_ptr whole_image[MAX_COMPONENTS];
  42. } my_coef_controller;
  43.  
  44. typedef my_coef_controller * my_coef_ptr;
  45.  
  46.  
  47. /* Forward declarations */
  48. METHODDEF boolean decompress_data
  49.     JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
  50. #ifdef D_MULTISCAN_FILES_SUPPORTED
  51. METHODDEF boolean decompress_read
  52.     JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
  53. METHODDEF boolean decompress_output
  54.     JPP((j_decompress_ptr cinfo, JSAMPIMAGE output_buf));
  55. #endif
  56.  
  57.  
  58. LOCAL void
  59. start_iMCU_row (j_decompress_ptr cinfo)
  60. /* Reset within-iMCU-row counters for a new row */
  61. {
  62.   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  63.  
  64.   /* In an interleaved scan, an MCU row is the same as an iMCU row.
  65.    * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
  66.    * But at the bottom of the image, process only what's left.
  67.    */
  68.   if (cinfo->comps_in_scan > 1) {
  69.     coef->MCU_rows_per_iMCU_row = 1;
  70.   } else {
  71.     if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
  72.       coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
  73.     else
  74.       coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
  75.   }
  76.  
  77.   coef->mcu_ctr = 0;
  78.   coef->MCU_vert_offset = 0;
  79. }
  80.  
  81.  
  82. /*
  83.  * Initialize for a processing pass.
  84.  */
  85.  
  86. METHODDEF void
  87. start_pass_coef (j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
  88. {
  89.   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  90.  
  91.   coef->iMCU_row_num = 0;
  92.   start_iMCU_row(cinfo);
  93.  
  94.   switch (pass_mode) {
  95.   case JBUF_PASS_THRU:
  96.     if (coef->whole_image[0] != NULL)
  97.       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  98.     coef->pub.decompress_data = decompress_data;
  99.     break;
  100. #ifdef D_MULTISCAN_FILES_SUPPORTED
  101.   case JBUF_SAVE_SOURCE:
  102.     if (coef->whole_image[0] == NULL)
  103.       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  104.     coef->pub.decompress_data = decompress_read;
  105.     break;
  106.   case JBUF_CRANK_DEST:
  107.     if (coef->whole_image[0] == NULL)
  108.       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  109.     coef->pub.decompress_data = decompress_output;
  110.     break;
  111. #endif
  112.   default:
  113.     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  114.     break;
  115.   }
  116. }
  117.  
  118.  
  119. /*
  120.  * Process some data in the single-pass case.
  121.  * Always attempts to emit one fully interleaved MCU row ("iMCU" row).
  122.  * Returns TRUE if it completed a row, FALSE if not (suspension).
  123.  *
  124.  * NB: output_buf contains a plane for each component in image.
  125.  * For single pass, this is the same as the components in the scan.
  126.  */
  127.  
  128. METHODDEF boolean
  129. decompress_data (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  130. {
  131.   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  132.   JDIMENSION MCU_col_num;    /* index of current MCU within row */
  133.   JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  134.   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  135.   int blkn, ci, xindex, yindex, yoffset, useful_width;
  136.   JSAMPARRAY output_ptr;
  137.   JDIMENSION start_col, output_col;
  138.   jpeg_component_info *compptr;
  139.   inverse_DCT_method_ptr inverse_DCT;
  140.  
  141.   /* Loop to process as much as one whole iMCU row */
  142.   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  143.        yoffset++) {
  144.     for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
  145.      MCU_col_num++) {
  146.       /* Try to fetch an MCU.  Entropy decoder expects buffer to be zeroed. */
  147.       jzero_far((void FAR *) coef->MCU_buffer[0],
  148.         (size_t) (cinfo->blocks_in_MCU * SIZEOF(JBLOCK)));
  149.       if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
  150.     /* Suspension forced; update state counters and exit */
  151.     coef->MCU_vert_offset = yoffset;
  152.     coef->mcu_ctr = MCU_col_num;
  153.     return FALSE;
  154.       }
  155.       /* Determine where data should go in output_buf and do the IDCT thing.
  156.        * We skip dummy blocks at the right and bottom edges (but blkn gets
  157.        * incremented past them!).  Note the inner loop relies on having
  158.        * allocated the MCU_buffer[] blocks sequentially.
  159.        */
  160.       blkn = 0;            /* index of current DCT block within MCU */
  161.       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  162.     compptr = cinfo->cur_comp_info[ci];
  163.     /* Don't bother to IDCT an uninteresting component. */
  164.     if (! compptr->component_needed) {
  165.       blkn += compptr->MCU_blocks;
  166.       continue;
  167.     }
  168.     inverse_DCT = cinfo->idct->inverse_DCT[compptr->component_index];
  169.     useful_width = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  170.                             : compptr->last_col_width;
  171.     output_ptr = output_buf[ci] + yoffset * compptr->DCT_scaled_size;
  172.     start_col = MCU_col_num * compptr->MCU_sample_width;
  173.     for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  174.       if (coef->iMCU_row_num < last_iMCU_row ||
  175.           yoffset+yindex < compptr->last_row_height) {
  176.         output_col = start_col;
  177.         for (xindex = 0; xindex < useful_width; xindex++) {
  178.           (*inverse_DCT) (cinfo, compptr,
  179.                   (JCOEFPTR) coef->MCU_buffer[blkn+xindex],
  180.                   output_ptr, output_col);
  181.           output_col += compptr->DCT_scaled_size;
  182.         }
  183.       }
  184.       blkn += compptr->MCU_width;
  185.       output_ptr += compptr->DCT_scaled_size;
  186.     }
  187.       }
  188.     }
  189.     /* Completed an MCU row, but perhaps not an iMCU row */
  190.     coef->mcu_ctr = 0;
  191.   }
  192.   /* Completed the iMCU row, advance counters for next one */
  193.   coef->iMCU_row_num++;
  194.   start_iMCU_row(cinfo);
  195.   return TRUE;
  196. }
  197.  
  198.  
  199. #ifdef D_MULTISCAN_FILES_SUPPORTED
  200.  
  201. /*
  202.  * Process some data: handle an input pass for a multiple-scan file.
  203.  * We read the equivalent of one fully interleaved MCU row ("iMCU" row)
  204.  * per call, ie, v_samp_factor block rows for each component in the scan.
  205.  * No data is returned; we just stash it in the virtual arrays.
  206.  * Returns TRUE if it completed a row, FALSE if not (suspension).
  207.  */
  208.  
  209. METHODDEF boolean
  210. decompress_read (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  211. {
  212.   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  213.   JDIMENSION MCU_col_num;    /* index of current MCU within row */
  214.   int blkn, ci, xindex, yindex, yoffset;
  215.   JDIMENSION total_width, start_col;
  216.   JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  217.   JBLOCKROW buffer_ptr;
  218.   jpeg_component_info *compptr;
  219.  
  220.   /* Align the virtual buffers for the components used in this scan. */
  221.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  222.     compptr = cinfo->cur_comp_info[ci];
  223.     buffer[ci] = (*cinfo->mem->access_virt_barray)
  224.       ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
  225.        coef->iMCU_row_num * compptr->v_samp_factor, TRUE);
  226.     /* Entropy decoder expects buffer to be zeroed. */
  227.     total_width = (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  228.                      (long) compptr->h_samp_factor);
  229.     for (yindex = 0; yindex < compptr->v_samp_factor; yindex++) {
  230.       jzero_far((void FAR *) buffer[ci][yindex], 
  231.         (size_t) (total_width * SIZEOF(JBLOCK)));
  232.     }
  233.   }
  234.  
  235.   /* Loop to process one whole iMCU row */
  236.   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  237.        yoffset++) {
  238.     for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
  239.      MCU_col_num++) {
  240.       /* Construct list of pointers to DCT blocks belonging to this MCU */
  241.       blkn = 0;            /* index of current DCT block within MCU */
  242.       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  243.     compptr = cinfo->cur_comp_info[ci];
  244.     start_col = MCU_col_num * compptr->MCU_width;
  245.     for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  246.       buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  247.       for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
  248.         coef->MCU_buffer[blkn++] = buffer_ptr++;
  249.       }
  250.     }
  251.       }
  252.       /* Try to fetch the MCU. */
  253.       if (! (*cinfo->entropy->decode_mcu) (cinfo, coef->MCU_buffer)) {
  254.     /* Suspension forced; update state counters and exit */
  255.     coef->MCU_vert_offset = yoffset;
  256.     coef->mcu_ctr = MCU_col_num;
  257.     return FALSE;
  258.       }
  259.     }
  260.     /* Completed an MCU row, but perhaps not an iMCU row */
  261.     coef->mcu_ctr = 0;
  262.   }
  263.   /* Completed the iMCU row, advance counters for next one */
  264.   coef->iMCU_row_num++;
  265.   start_iMCU_row(cinfo);
  266.   return TRUE;
  267. }
  268.  
  269.  
  270. /*
  271.  * Process some data: output from the virtual arrays after reading is done.
  272.  * Always emits one fully interleaved MCU row ("iMCU" row).
  273.  * Always returns TRUE --- suspension is not possible.
  274.  *
  275.  * NB: output_buf contains a plane for each component in image.
  276.  */
  277.  
  278. METHODDEF boolean
  279. decompress_output (j_decompress_ptr cinfo, JSAMPIMAGE output_buf)
  280. {
  281.   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  282.   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  283.   JDIMENSION block_num;
  284.   int ci, block_row, block_rows;
  285.   JBLOCKARRAY buffer;
  286.   JBLOCKROW buffer_ptr;
  287.   JSAMPARRAY output_ptr;
  288.   JDIMENSION output_col;
  289.   jpeg_component_info *compptr;
  290.   inverse_DCT_method_ptr inverse_DCT;
  291.  
  292.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  293.        ci++, compptr++) {
  294.     /* Don't bother to IDCT an uninteresting component. */
  295.     if (! compptr->component_needed)
  296.       continue;
  297.     /* Align the virtual buffer for this component. */
  298.     buffer = (*cinfo->mem->access_virt_barray)
  299.       ((j_common_ptr) cinfo, coef->whole_image[ci],
  300.        coef->iMCU_row_num * compptr->v_samp_factor, FALSE);
  301.     /* Count non-dummy DCT block rows in this iMCU row. */
  302.     if (coef->iMCU_row_num < last_iMCU_row)
  303.       block_rows = compptr->v_samp_factor;
  304.     else {
  305.       /* NB: can't use last_row_height here, since may not be set! */
  306.       block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  307.       if (block_rows == 0) block_rows = compptr->v_samp_factor;
  308.     }
  309.     inverse_DCT = cinfo->idct->inverse_DCT[ci];
  310.     output_ptr = output_buf[ci];
  311.     /* Loop over all DCT blocks to be processed. */
  312.     for (block_row = 0; block_row < block_rows; block_row++) {
  313.       buffer_ptr = buffer[block_row];
  314.       output_col = 0;
  315.       for (block_num = 0; block_num < compptr->width_in_blocks; block_num++) {
  316.     (*inverse_DCT) (cinfo, compptr, (JCOEFPTR) buffer_ptr,
  317.             output_ptr, output_col);
  318.     buffer_ptr++;
  319.     output_col += compptr->DCT_scaled_size;
  320.       }
  321.       output_ptr += compptr->DCT_scaled_size;
  322.     }
  323.   }
  324.  
  325.   coef->iMCU_row_num++;
  326.   return TRUE;
  327. }
  328.  
  329. #endif /* D_MULTISCAN_FILES_SUPPORTED */
  330.  
  331.  
  332. /*
  333.  * Initialize coefficient buffer controller.
  334.  */
  335.  
  336. GLOBAL void
  337. jinit_d_coef_controller (j_decompress_ptr cinfo, boolean need_full_buffer)
  338. {
  339.   my_coef_ptr coef;
  340.   int ci, i;
  341.   jpeg_component_info *compptr;
  342.   JBLOCKROW buffer;
  343.  
  344.   coef = (my_coef_ptr)
  345.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  346.                 SIZEOF(my_coef_controller));
  347.   cinfo->coef = (struct jpeg_d_coef_controller *) coef;
  348.   coef->pub.start_pass = start_pass_coef;
  349.  
  350.   /* Create the coefficient buffer. */
  351.   if (need_full_buffer) {
  352. #ifdef D_MULTISCAN_FILES_SUPPORTED
  353.     /* Allocate a full-image virtual array for each component, */
  354.     /* padded to a multiple of samp_factor DCT blocks in each direction. */
  355.     /* Note memmgr implicitly pads the vertical direction. */
  356.     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  357.      ci++, compptr++) {
  358.       coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
  359.     ((j_common_ptr) cinfo, JPOOL_IMAGE,
  360.      (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  361.                 (long) compptr->h_samp_factor),
  362.      compptr->height_in_blocks,
  363.      (JDIMENSION) compptr->v_samp_factor);
  364.     }
  365. #else
  366.     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
  367. #endif
  368.   } else {
  369.     /* We only need a single-MCU buffer. */
  370.     buffer = (JBLOCKROW)
  371.       (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  372.                   MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
  373.     for (i = 0; i < MAX_BLOCKS_IN_MCU; i++) {
  374.       coef->MCU_buffer[i] = buffer + i;
  375.     }
  376.     coef->whole_image[0] = NULL; /* flag for no virtual arrays */
  377.   }
  378. }
  379.